#include "Menu.h"

static MenuState_t currentState = MENU_MAIN;
static uint8_t currentSelection = 0;
static MenuItem_t *menuItems[5][10]; // 支持最多5个菜单状态，每个状态10个选项
static uint8_t itemCount[5] = {0};


/*
 * 菜单初始化模块
 * 各个接口初始化以及主菜单定义
 *
 *
 */
void Menu_Init(void) {
    GPIO_LED_Init();
    Display_Init();
    GPIO_KEY_Init();
    Display_CLS(U16_WHITE);

    // 初始化主菜单
    static const MenuItem_t mainItems[] = {
        {"MENU_PARAM_SETTING", MENU_PARAM_SETTING, NULL},
        {"MENU_SUBJECT_SELECTION", MENU_SUBJECT_SELECTION, NULL},
        {"MENU_MODULE_TESTING", MENU_MODULE_TESTING, NULL}
    };

    static const MenuItem_t selItems[] = {
            {"SUBJECT_1",SUBJECT_1,NULL},
            {"SUBJECT_2",SUBJECT_2,NULL},
            {"SUBJECT_3",SUBJECT_3,NULL},
            {"SUBJECT_3",CAMERA,NULL}
    };

    for(int i=0; i<3; i++) {
        Menu_Control_AddItem(MENU_MAIN, &mainItems[i]);
    }

    for(int i=0; i<4;  i++) {
        Menu_Control_AddItem(MENU_SUBJECT_SELECTION,&selItems[i]);
    }
}

void Menu_Control_AddItem(MenuState_t state, const MenuItem_t *item) {
    if(state < 5 && itemCount[state] < 10) {
        menuItems[state][itemCount[state]++] = (MenuItem_t *)item;
    }
}

/*
 * 处理按键输入
 *
 */

void Menu_HandleInput(void) {
    static uint64 lastKeyTime = 0;
    uint64 currentTime = IfxStm_get(&MODULE_STM0);
    
    // 按键消抖处理(20ms)
    if(currentTime - lastKeyTime < 20) {
        return;
    }
    
    // 处理KEY2(上)
    if(!KEY_Read(KEY2)) {
        currentSelection = (currentSelection > 0) ? currentSelection-1 : itemCount[currentState]-1;
        lastKeyTime = currentTime;
        return;
    }
    
    // 处理KEY0(下)
    if(!KEY_Read(KEY0)) {
        currentSelection = (currentSelection < itemCount[currentState]-1) ? currentSelection+1 : 0;
        lastKeyTime = currentTime;
        return;
    }
    
    // 处理KEY1(确认/长按返回)
    static uint64 key1PressTime = 0;
    if(!KEY_Read(KEY1)) {
        if(key1PressTime == 0) {
            key1PressTime = currentTime;
        } 
        else if(currentTime - key1PressTime > 1000) {  // 长按1秒
            currentState = MENU_MAIN;
            currentSelection = 0;
            key1PressTime = 0;
            lastKeyTime = currentTime;
            return;
        }
    } 
    else {
        if(key1PressTime != 0) {  // 短按处理
            if(menuItems[currentState][currentSelection]->action != NULL) {
                menuItems[currentState][currentSelection]->action();
            }
            currentState = menuItems[currentState][currentSelection]->nextState;
            currentSelection = 0;
        }
        key1PressTime = 0;
    }
    
    lastKeyTime = currentTime;
}

/*
 * 菜单显示更新
 */

void Menu_UpdateDisplay(void) {
    // 根据当前菜单状态设置不同的背景色
    uint16_t bgColor;
    switch(currentState) {
        case MENU_MAIN: 
            bgColor = U16_WHITE; 
            break;
        case MENU_PARAM_SETTING: 
            bgColor = U16_BLUE;
            break;
        case MENU_SUBJECT_SELECTION: 
            bgColor = U16_GREEN;
            break;
        case MENU_MODULE_TESTING: 
            bgColor = U16_YELLOW;
            break;
        case SUBJECT_WAIT:
            bgColor = U16_GREEN;
            break;
        default: 
            bgColor = U16_WHITE;
    }
    
    // 清屏并使用当前菜单的背景色
    Display_CLS(bgColor);
    
    // 添加菜单标题
    char title[50];
    switch(currentState) {
        case MENU_MAIN: strcpy(title, "MAIN_MENU"); break;
        case MENU_PARAM_SETTING: strcpy(title, "MENU_PARAM_SETTING"); break;
        case MENU_SUBJECT_SELECTION: strcpy(title, "MENU_SUBJECT_SELECTION"); break;
        case MENU_MODULE_TESTING: strcpy(title, "MENU_MODULE_TESTING"); break;
        case SUBJECT_1: strcpy(title, "SUBJECT_1"); break;
        case SUBJECT_2: strcpy(title, "SUBJECT_2"); break;
        case SUBJECT_3: strcpy(title, "SUBJECT_3"); break;
        default: strcpy(title, "Unknow_Menu");
    }
    


    // 显示菜单标题
    char displayBuffer[50];
    sprintf(displayBuffer, "== %s ==", title);
    Display_showString(1, 1, displayBuffer, U16_BLACK, bgColor, 16);
    
    // 显示菜单项
    for(int i=0; i<itemCount[currentState]; i++) {
        sprintf(displayBuffer, "%s", menuItems[currentState][i]->text);
        Display_showString(1, 4+i*4, displayBuffer, 
            i==currentSelection ? U16_RED : U16_BLACK,
            bgColor, 16);
    }
}

void Menu_Fuction(void)
{
    switch(currentState){
        case SUBJECT_1:
            LED_Ctrl(LEDALL, OFF);
            LED_Ctrl(LED0, ON);
            Subject1();
            break;
        case SUBJECT_2:
            LED_Ctrl(LEDALL, OFF);
            LED_Ctrl(LED1, ON);
            Subject2();
            break;
        case SUBJECT_3:
            LED_Ctrl(LEDALL, ON);
            Subject3();
            break;
        case CAMERA:
            Test_CAMERA();
            break;
        default:return;
    }
}


/*
 * 菜单主程序
 *
 */

void Menu_Run(void) {
    GPIO_LED_Init();
    while(1) {
        Menu_HandleInput();
        Menu_UpdateDisplay();
        Menu_Fuction();
        Delay_Ms(50);
    }
}

